// Language module, added by regret

/* changelog:
 update 2: add string cache
 update 1: create
*/

#include "burner.h"

bool bLanguageActive = false;
wchar_t szLanguage[MAX_PATH];
HMODULE hLanguage = NULL;

// ----------------------------------------------------------------------------

INT_PTR localiseDialogBox(int id, HWND parent, DLGPROC lpDialogFunc)
{
	INT_PTR p = DialogBox(hLanguage ? hLanguage : hAppInst, MAKEINTRESOURCE(id), parent, lpDialogFunc);
	return p;
}

HWND localiseCreateDialog(int id, HWND parent, DLGPROC lpDialogFunc)
{
	HWND hwnd = CreateDialog(hLanguage ? hLanguage : hAppInst, MAKEINTRESOURCE(id), parent, lpDialogFunc);
	return hwnd;
}

HMENU localiseLoadMenu(int id)
{
	HMENU hmenu = LoadMenu(hLanguage ? hLanguage : hAppInst, MAKEINTRESOURCE(id));
	return hmenu;
}

HBITMAP localiseLoadBitmap(int id)
{
	HBITMAP hbitmap = LoadBitmap(hLanguage ? hLanguage : hAppInst, MAKEINTRESOURCE(id));
	return hbitmap;
}

int localiseString(UINT id, LPTSTR buffer, int maxsize)
{
	return LoadString(hLanguage ? hLanguage : hAppInst, id, buffer, maxsize);
}

wchar_t* localiseStringEx(UINT id, bool translate)
{
	static wchar_t loadstr[2048] = L"";

	if (translate) {
		localiseString(id, loadstr, sizearray(loadstr));
	} else {
		LoadString(hAppInst, id, loadstr, sizearray(loadstr));
	}

	return loadstr;
}

// ----------------------------------------------------------------------------

HMODULE localiseInstance()
{
	return hLanguage ? hLanguage : hAppInst;
}

void localiseExit()
{
	// Unload the dll
	if (hLanguage) {
		FreeLibrary(hLanguage);
		hLanguage = NULL;
	}

	bLanguageActive = false;
}

int localiseInit(wchar_t* lanaugae)
{
	localiseExit();

	if (!lanaugae || !lanaugae[0]) {
		return 0;
	}

	hLanguage = LoadLibrary(lanaugae);
	if (!hLanguage) {
		FBAPopupAddText(PUF_TYPE_WARNING, L"Language dll load failed!");
		FBAPopupDisplay(PUF_TYPE_WARNING);
		return 1;
	}

	wcsncpy(szLanguage, lanaugae, sizearray(szLanguage));
	bLanguageActive = true;
	return 0;
}

// ---------------------------------------------------------------------------
// Dialog box to load language

int localiseLoad()
{
	if (commonFileDialog(hScrnWnd, false, localiseStringEx(IDS_LOCAL_SELECT), L"Language (*.dll)\0*.dll\0All Files (*.*)\0*.*\0\0", L"lang", L"dll")) {
		return 1;
	}
	return localiseInit(commonChoice);
}
